﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TTQT;
using TTQT.TTQT_Code;

public partial class SanPham_NoiSX : MasterControl //System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if(isRole("ksv"))
        //{
        // cmdAddNew.Visible = false;
        //}
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            bindGrid("");
            visiblePanels(true, false);
            return;
        }
    }
    private void bindGrid(string ma)
    {
        try
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            if (ma == null || ma == string.Empty)
            {
                ds.SelectCommand = "SELECT id,noisx from sanpham_noisx";
            }
            else
            {
                ds.SelectCommand = "SELECT id,noisx from sanpham_noisx"
                       + " WHERE id='" + ma + "'";
            }
            DataTable rsDDL = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();
            DataTable tb = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();
            datagrid.DataSource = tb;
            datagrid.DataBind();
        }
        catch (Exception)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        this.txtMa.Text = string.Empty;
        this.txtNoiSX.Text = string.Empty;
        this.txtMa.ReadOnly = false;
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng
            if (txtMa.Text.Trim() != string.Empty)
            {
                ds.UpdateCommand = "UPDATE SanPham_NoiSX"
                       + " SET "
                       + " noisx =N'" + txtNoiSX.Text.Trim()
                       + "' WHERE id='" + txtMa.Text.Trim() + "'";
                ds.Update();
                bindGrid("");
                visiblePanels(true, false);
            }
            // Nếu là thêm mới thì ID sẽ rỗng
            else if (txtMa.Text.Trim() == string.Empty)
            {
                if (CheckExisted(txtMa.Text.Trim()))
                {
                    this.lblError.Text = "Đã tồn tại mã " + txtMa.Text.Trim() + ". Hãy nhập lại mã loại ấn chỉ khác! ";
                }
                else
                {

                    ds.InsertCommand = "INSERT INTO SanPham_NoiSX (NoiSX) "
                           + " VALUES(N'" + txtNoiSX.Text.Trim()
                           + "') ";
                    ds.Insert();
                    bindGrid("");
                    visiblePanels(true, false);
                    this.txtMa.ReadOnly = true;
                }
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = "Loi khi cap nhat. Nhap lai!";
            //Log methods here
        }
    }

    private bool CheckExisted(string ma)
    {
        try
        {
            return false;
        }catch{}
        try
        {
            ds.SelectCommand = "SELECT  id FROM SanPham_NoiSX"
                  + " WHERE id='" + ma + "'";
            DataTable tb = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();
            return tb.Rows.Count > 0;
        }
        catch (Exception ex)
        {
            return false;
        }

    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        this.txtMa.ReadOnly = true;
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "cmdEdit")
            {
                visiblePanels(false, true);
                ds.SelectCommandType = SqlDataSourceCommandType.Text;
                ds.SelectCommand = "SELECT * "
                     + "FROM  SanPham_NoiSX "
                     + " where id='" + e.Item.Cells[0].Text + "'";
                DataTable rs = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();

                this.txtMa.Text = rs.Rows[0][0].ToString();
                this.txtNoiSX.Text = rs.Rows[0]["NoiSX"].ToString();

                this.lblError.Text = "";
                this.lblUpdateStatus.Text = "";

                // Save old info
            }
            else if (e.CommandName == "cmdDelete")
            {
                SanPham_NoiSXDAO dao=new SanPham_NoiSXDAO();
                if(dao.chayCauSQLScalarInt("select * from sanpham where noisx=" + e.Item.Cells[0].Text)>0)
                {
                    MyWebUtils.MyWebUtil.alert(this.Page,"Nơi sản xuất này đã được gán cho một số sản phẩm. Hãy cập nhật lại sản phẩm trước khi xóa nơi sản xuất.");
                    return;
                }
                ds.DeleteCommand = "delete SanPham_NoiSX where id='" + e.Item.Cells[0].Text + "'";
                ds.Delete();
                bindGrid("");
            }
        }
        catch (SqlException ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = "Loi khi truy xuat du lieu. Nhap lai!";
        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", "delete_Confirm");
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid("");
    }

}
